<?php
namespace Home\Controller;
use Think\Page;

class NewsController extends BaseController
{
    protected $mdlCategory = null;
    protected $mdlSinglePage = null;

    public function __construct()
    {
        parent::__construct();
        $cat_list = M('NewsCategory')->order('sort asc')->select();
        $this->assign('cat_list', $cat_list);
    }

    private function _getDefaulCatId()
    {
        $id = 0;
        $list = M('NewsCategory')->select();
        if ($list) {
            $info = $list[0];
            $id = $info['id'];
        }
        return $id;
    }

    public function indexAction()
    {
        $defaultCatId = $this->_getDefaulCatId();
        $cat_id = I('cat_id', $defaultCatId, 'intval');
        $cat_info = M('NewsCategory')->find($cat_id);
        $News = M('News');
        $condition = array(
            'cat_id' => $cat_id,
        );
        $count = $News->where($condition)->count();
        $Page = new Page($count, 50);
        $Page->setConfig('header', '');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '');
        $show = $Page->show();
        $list = $News->where($condition)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('page', $show);
        $this->assign('list', $list);
        $global_config = array(
            'title' => $cat_info['name'] . '-' . C('SITE_NAME'),
            'keywords' => $cat_info['name'],
            'description' => $cat_info['name'],
        );
        $this->assign('cat_id', $cat_id);
        $this->assign('list', $list);
        $this->assign('global_config', $global_config);
        $this->display('soon');
    }



    public function detailAction()
    {
        $cat_id = I('get.cat_id', 0);
        $id = I('get.id', 0);
        $news_info = M('News')->find($id);
        $global_config = array(
            'title' => $news_info['title'] . '-' . C('SITE_NAME'),
            'keywords' => $news_info['title'],
            'description' => $news_info['title'],
        );
        $cat_list = M('NewsCategory')->order('sort asc')->select();
        $this->assign('cat_list', $cat_list);
        $this->assign('cat_id', $cat_id);
        $this->assign('news_info', $news_info);
        $this->assign('global_config', $global_config);
        $this->display();
    }

    public function photoAction()
    {
        $this->assign('sidebar_flag', 'photo');
        $this->display('');
    }

    public function photoAddAction()
    {
        if (IS_POST) {
            $return = array('code' => 200, 'message' => '');
            $Photo = D('Photo');
            $data = $Photo->create($_POST['Photo']);
            if (false !== $data) {
                $insertId = $Photo->add();
                if (false !== $insertId) {
                    echo json_encode($return);
                    exit;
                } else {
                    $return = array(
                        'code' => '4000',
                        'message' => '上传失败!',
                    );
                    echo json_encode($return);
                    exit;
                }
            } else {
                $return = array(
                    'code' => '4001',
                    'message' => $Photo->getError(),
                );
                echo json_encode($return);
                exit;
            }
        } else {
            $this->assign('sidebar_flag', 'photo');
            $this->display();
        }
    }

    /**
     * 编辑器上传处理
     */
    public function uploadAction()
    {
        $config = array(
            'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'photo/',
            'subPath' => 'photo/',
        );
        vendor('Kindeditor.UploadJson', '', '.class.php');
        $UploadJson = new \UploadJson($config);
        $UploadJson->upload($_FILES['file']);
    }

    public function ganyanAction()
    {
        $this->assign('sidebar_flag', 'ganyan');
        $this->display('soon');
    }
}